// hdu5730
// 题意：
// 给定n(<=10^5)个非负整数ai，现在有一个长度为n的项链展开成一条线。
// 你可以对它进行任意划分，如果划分成m段，每段长度为b1, b2, ..., bm，
// 那么最后的价值就是b1*b2*...*bm，问最后任意划分下总的价值是多少？
//
// 题解：
// 很明显这可以写出一个dp，dp[i]=sigma(dp[j]*a[i-j])，0<=j<i，
// 然后i只和前面所有项相关，并且符合卷积的形式。考虑cdq分治+fft。
// 分治的时候用fft算出前一半对后一半的贡献，然后递归处理两个子区间。
//
// ml:run = $bin < 1008.in > out
#include <iostream>
#include <cstring>
#include <complex>
#include <cmath>
#include <cstring>

using ll = long long;
int const mo = 313;
int const maxn = 4 * 100007;
ll a[maxn];
ll ta[maxn];
ll ans[maxn];
ll tmp[maxn];
int n;

namespace fft
{
	using base = std::complex<long double>;
	long double const pi = std::acos(-1);

	int rev[maxn];
	base wlen_pw[maxn];

	void fft(base a[], int n, bool invert)
	{
		for (int i = 0; i < n; i++)
			if (i < rev[i]) std::swap(a[i], a[rev[i]]);

		for (int len = 2; len <= n; len *= 2) {
			long double ang = (2 * pi / len) * (invert ? -1 : 1);
			int len2 = len / 2;
			base wlen(std::cos(ang), std::sin(ang));
			wlen_pw[0] = base(1, 0);
			for (int i = 1; i < len2; i++)
				wlen_pw[i] = wlen_pw[i - 1] * wlen;

			for (int i = 0; i < n; i += len) {
				base t, *pu = a + i, *pv = a + i + len2, *pu_end = a + i + len2, *pw = wlen_pw;
				for (; pu != pu_end; ++pu, ++pv, ++pw) {
					t = *pv * *pw;
					*pv = *pu - t;
					*pu += t;
				}
			}
		}

		if (!invert) return;
		for (int i = 0; i < n; i++) a[i] /= n;
	}

	base fa[maxn], fb[maxn];
	int calc_last = -1;

	void calc_rev(int n)
	{
		if (n == calc_last) return;
		int logn = 0;
		while ((1 << logn) < n) logn++;
		for (int i = 0; i < n; i++) {
			rev[i] = 0;
			for (int j = 0; j < logn; j++)
				if (i & (1 << j))
					rev[i] |= 1 << (logn - j - 1);
		}
		calc_last = n;
	}

	template <class T>
	void multiply(T a[], int n1, T b[], int n2, T res[], int & len)
	{
		int n = 1;
		while (n < n1 || n < n2) n *= 2;
		n *= 2; len = n;
		for (int i = 0; i < n1; i++) fa[i] = a[i];
		for (int i = n1; i < n; i++) fa[i] = 0;
		for (int i = 0; i < n2; i++) fb[i] = b[i];
		for (int i = n2; i < n; i++) fb[i] = 0;

		calc_rev(n);
		fft(fa, n, false); fft(fb, n, false);
		for (int i = 0; i < n; i++) fa[i] *= fb[i];
		fft(fa, n, true);

		for (int i = 0; i < n; i++) {
            res[i] = std::round(fa[i].real());
            res[i] %= mo;
        }
	}
}

void calc(int l, int r)
{
    if (l == r) { ans[l] = (ans[l] + a[l]) % mo; return; }
    int mid = (l + r) / 2;
    calc(l, mid);
    for (int i = l; i <= mid; i++) ta[i - l + 1] = ans[i];
    int tn = r - l + 2;

    fft::multiply(a, r - l + 1, ta, mid - l + 2, tmp, tn);

    for (int i = mid + 1; i <= r; i++) {
        ans[i] += tmp[i - l + 1];
        ans[i] %= mo;
    }
    calc(mid + 1, r);
}

int work()
{
    calc(1, n);
    /* for (int i = 0; i <= n; i++) std::cout << ans[i] << " "; */
    return ans[n] % mo;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    while (std::cin >> n && n) {
        std::memset(ans, 0, sizeof(ans));
        for (int i = 1; i <= n; i++) {
            std::cin >> a[i];
            a[i] %= mo;
        }
        std::cout << work() << "\n";
    }
}

